var EventEmitter = require('events').EventEmitter; 
var extend = require('util')._extend;
var logger = require('log4js').getLogger();
var _ = require('underscore');



var _DefaultsOpts = {
    interval: 3000
};

//-------------------Monitor Class---------------------

function Monitor(mid,opts){
  this.id = mid;
  this.dog = new EventEmitter(); 
  this.options = _.extend(_DefaultsOpts, opts);
  this.status = 0;


  
  //-------- start event
  this.dog.on('start', function(opt) { 
    logger.info('task will be start.');
    this.intervalId = setInterval(function(){
      var detector = opts.detector.runner;
      var callback = opts.detector.callback;
      var dd = opts.detector.data;
      detector.detect(dd,callback);
    }, opt.interval);
  }); 

  //-------- check event
  this.dog.on('stop', function() { 
    logger.info('task will be stop.');

    clearInterval(this.intervalId);
    this.intervalId = null;
  });     

}



Monitor.prototype.start = function (){
  this.dog.emit('start', this.options);
};

Monitor.prototype.stop = function (){
  this.dog.emit('stop');
};

Monitor.prototype.getStatus = function (){
  return {
    status: this.status,
    config: this.options
  };
};


//-------------------Monitor Class---------------------

module.exports = Monitor;